package com.ykdl.member.kid.fittool;

import java.util.List;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.ykdl.member.kid.R;
import com.ykdl.member.http.Network;
import com.ykdl.member.kid.adapters.FeedReportDayAdapter;
import com.ykdl.member.kid.adapters.FeedReportWeekAdapter;
import com.ykdl.member.kid.beans.FeedReportDayBean;
import com.ykdl.member.kid.beans.FeedReportListDayBean;
import com.ykdl.member.kid.beans.FeedReportListWeekBean;
import com.ykdl.member.kid.beans.FeedReportWeekBean;
import com.ykdl.member.kid.widget.RefreshListView;

public class FeedReportActivity extends BaseActivity implements OnClickListener{

	private static final String TAG = FeedReportActivity.class.getSimpleName();

	private RefreshListView mDayListView;
	private RefreshListView mWeekListView;
	private View mLayerTitle;
	private TextView mTxtDay, mTxtWeek;
	private ProgressDialog mProgressDialog;
	
	private FeedReportDayAdapter mDayAdapter;
	private FeedReportWeekAdapter mWeekAdapter;
	
	private int dCousor = 0;
	private int wCousor = 0;
	private int count = 20;
	
	private boolean dayPage = true;
	private FeedDayListTag mDayTag;
	private FeedWeekListTag mWeekTag;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_feed_report);
		initView();
		mDayAdapter = new FeedReportDayAdapter(this);
		mWeekAdapter = new FeedReportWeekAdapter(this);
		mDayTag = new FeedDayListTag();
		mWeekTag = new FeedWeekListTag();
		mProgressDialog = ProgressDialog.show(this, null, getString(R.string.loading_data), true, true);
		Network.getInstance().getFeedReportListByDay(dCousor, count, mDayTag);
	}
	
	private void initView(){
		findViewById(R.id.btnBack).setOnClickListener(this);
		mDayListView = (RefreshListView) findViewById(R.id.dayListView);
		mDayListView.setAdapter(mDayAdapter);
		mWeekListView = (RefreshListView) findViewById(R.id.weekListView);
		mWeekListView.setAdapter(mWeekAdapter);
		
		mLayerTitle = findViewById(R.id.layerTitle);
		mTxtDay = (TextView) findViewById(R.id.txtDay);
		mTxtDay.setOnClickListener(this);
		mTxtWeek = (TextView) findViewById(R.id.txtWeek);
		mTxtWeek.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnBack:
			finish();
			break;
		case R.id.txtDay:
			if (dayPage) {
				
			} else {
				if (mDayAdapter.getCount() == 0) {
					dCousor = 0;
					Network.getInstance().getFeedReportListByDay(dCousor, count, new FeedDayListTag());
				}
				mLayerTitle.setBackgroundResource(R.drawable.bg_day_week);
				mTxtDay.setTextColor(getResources().getColor(R.color.white));
				mTxtWeek.setTextColor(getResources().getColor(R.color.orange));
				mDayListView.setVisibility(View.VISIBLE);
				mWeekListView.setVisibility(View.GONE);
				dayPage = true;
			}
			break;
		case R.id.txtWeek:
			if (dayPage) {
				if (mWeekAdapter.getCount() == 0) {
					wCousor = 0;
					Network.getInstance().getFeedReportListByWeek(wCousor, count, mWeekTag);
				}
				mLayerTitle.setBackgroundResource(R.drawable.bg_week_day);
				mTxtDay.setTextColor(getResources().getColor(R.color.orange));
				mTxtWeek.setTextColor(getResources().getColor(R.color.white));
				mDayListView.setVisibility(View.GONE);
				mWeekListView.setVisibility(View.VISIBLE);
				dayPage = false;
			} else {
				 
			}
			break;
		default:
			break;
		}
	}
	
	private class FeedDayListTag implements ITag<FeedReportListDayBean> {

		@Override
		public void refreshUI(FeedReportListDayBean t, DataState ds) {
			if (mProgressDialog != null)
				mProgressDialog.dismiss();
			if (t != null && t.getList() != null) {
				List<FeedReportDayBean> beans = t.getList();
				mDayAdapter.addBeans(beans);
				mDayListView.setAdapter(mDayAdapter);
				if (t.getNext_cursor() == t.getTotal_number()) {
					mDayListView.setMoreButtoIsGon(true);
				} else {
					dCousor = t.getNext_cursor();
				}
			} else {
				Toast.makeText(FeedReportActivity.this, R.string.loaded_data_fail, Toast.LENGTH_SHORT).show();
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			if (mProgressDialog != null)
				mProgressDialog.dismiss();
			if (TextUtils.isEmpty(parseError.getErrMsg())) {
				Toast.makeText(FeedReportActivity.this, R.string.loaded_data_fail, Toast.LENGTH_SHORT).show();
				return true;
			} else {
				Toast.makeText(FeedReportActivity.this, parseError.getErrMsg(), Toast.LENGTH_SHORT).show();
				return false;
			}
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	private class FeedWeekListTag implements ITag<FeedReportListWeekBean> {

		@Override
		public void refreshUI(FeedReportListWeekBean t, DataState ds) {
			if (mProgressDialog != null)
				mProgressDialog.dismiss();
			if (t != null && t.getList() != null) {
				
				List<FeedReportWeekBean> beans = t.getList();
				mWeekListView.setAdapter(mWeekAdapter);
				mWeekAdapter.addBeans(beans);
				mWeekAdapter.notifyDataSetChanged();
				if (t.getNext_cursor() == t.getTotal_number()) {
					mWeekListView.setMoreButtoIsGon(true);
				} else {
					wCousor = t.getNext_cursor();
				}
			} else {
				Toast.makeText(FeedReportActivity.this, R.string.loaded_data_fail, Toast.LENGTH_SHORT).show();
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			if (mProgressDialog != null)
				mProgressDialog.dismiss();
			if (TextUtils.isEmpty(parseError.getErrMsg())) {
				Toast.makeText(FeedReportActivity.this, R.string.loaded_data_fail, Toast.LENGTH_SHORT).show();
				return true;
			} else {
				Toast.makeText(FeedReportActivity.this, parseError.getErrMsg(), Toast.LENGTH_SHORT).show();
				return false;
			}
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	
}
